package com.dyh.quickdemozwei.hxfz;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.dyh.quickdemozwei.util.DateUtil;
import com.google.common.collect.Lists;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.util.CollectionUtils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class TestExcel2 {


    @Setter@Getter@AllArgsConstructor
   public static   class EeData{
        @ExcelProperty("字段1")
        String a;

        @ExcelProperty("字段2")
        String b;

        public String getA() {
            return a;
        }

        public void setA(String a) {
            this.a = a;
        }

        public String getB() {
            return b;
        }

        public void setB(String b) {
            this.b = b;
        }
    }

    @Setter@Getter
    public static class Ctrl<R,D>{
        File file;
        Class<? extends D> cls;
        R req;
        int pageNo;
        int pageSize;
        Function<R,List<D>> queryFc;
        BiConsumer<Integer,R> nextPageRefresher;
    }

//    public <R,D> void pageOutExcel(Ctrl<R,D> ctrl) throws IOException {
//        //当前页码
//
//
//        ExcelWriter writer =null;
//        WriteSheet writeSheet = null;
//
//        OutputStream os = new FileOutputStream(ctrl.getFile());
//        for (int i=0;;i++){
//            //doWrite
//            List data = ctrl.getQueryFc().apply(ctrl.getReq());
//
////            File tempFile = new File(String.format("\"/Users/yuhaodong/Desktop/ouput/yyy_p%s.xlsx\"",i));
//
//
////            if (CollectionUtils.isEmpty(data)) {
////                //数据为0的时候也需要写入，防止excel文件异常
////
////                EasyExcel.write(ctrl.getFile(), ctrl.getCls())
////                        .sheet().doWrite(data);
////
////                break;
////            }
//
////            if (writer==null){
//                writer = EasyExcel.write(os).build();
////                writeSheet = EasyExcel.writerSheet(0).build();
////
////            }
//            int dataSize = 0;
//            if (!CollectionUtils.isEmpty(data)){
////                writer.write(data, writeSheet);
//                dataSize = data.size();
////                data.clear();//
//                writer.write(data, EasyExcel.writerSheet(0).head(ctrl.getCls()).build());
//
//            }
//
//            //更新页码
//
//            ctrl.setPageNo(ctrl.getPageNo()+1);
//            ctrl.getNextPageRefresher().accept(ctrl.getPageNo(),ctrl.getReq());
//
//            if (dataSize<ctrl.getPageSize())
//                break;
//        }
//
//        writer.finish();
//        os.close();
//        //数据写入完毕关闭资源
////        excelWriter.finish();
//    }


    public <R,D> void pageOutExcel(Ctrl<R,D> ctrl){
        //当前页码

        ExcelWriter excelWriter = EasyExcel.write(ctrl.getFile(),ctrl.getCls()).build();

        for (;;){
            //doWrite
            List data = ctrl.getQueryFc().apply(ctrl.getReq());



            //读取的而数据写入excel
            excelWriter.write(data, EasyExcel.writerSheet(0).head(ctrl.getCls()).build());

            //更新页码

            ctrl.setPageNo(ctrl.getPageNo()+1);
            ctrl.getNextPageRefresher().accept(ctrl.getPageNo(),ctrl.getReq());

            if (data.size()<ctrl.getPageSize())
                break;
        }

        //数据写入完毕关闭资源
        excelWriter.finish();
    }

    @Setter@Getter
    public static class Request{
        int page;

    }



    @Test
    public void test() throws IOException {

        File file = new File("/Users/yuhaodong/Desktop/ouput/yyy.xlsx");


        List<EeData> fullPage = Lists.newArrayList(
                new EeData("dawd","1231414"),
                new EeData("dawd","1231414"),
                new EeData("dawd","1231414"),
                new EeData("dawd","1231414"),
                new EeData("dawd","1231414"),
                new EeData("dawd","1231414"),
                new EeData("dawd","1231414"),
                new EeData("dawd","1231414"),
                new EeData("dawd","1231414"),
                new EeData("dawd","1231414")
        );


        List<EeData> lastPage = Lists.newArrayList(
                new EeData("123","1231414"),
                new EeData("123","1231414"),
                new EeData("23","1231414"));

        Request req = new Request();

        Ctrl<Request,EeData> ctrl = new Ctrl<>();
        ctrl.setCls(EeData.class);
        ctrl.setFile(file);
        ctrl.setReq(req);
        ctrl.setPageSize(10);

        ctrl.setQueryFc(request -> {
            if (request.getPage()<7){
                return fullPage;
            }
            else
                return lastPage;
        });

        ctrl.setNextPageRefresher((i,r)->r.setPage(i));

        pageOutExcel(ctrl);


    }


    @Test public void testxxx(){
        System.out.println(DateUtil.date2str(new Date(new Date().getTime()+172800*1000)));

    }

    @Test public void testxxxyyyy(){

        String s1 = sdf0.format(DateUtil.str2Date("2024-02-04 14:26:20"));
        String s2 = sdf0.format(DateUtil.str2Date("2024-02-04 11:26:20"));
        String s3 = sdf0.format(DateUtil.str2Date("2024-02-05 01:26:20"));

        System.out.println();
    }

    SimpleDateFormat sdf0 = new SimpleDateFormat(
//                "yyyy-MM-dd HH:mm:ss"
            "yyyy-MM-dd 00:00:00"
    );



    @Test public void testx1(){

        f0:
        for (int i=0;i<10;i++){

           for (int j=0;j<3;j++){
               if (
                       (i ==2 || i==4)
                       && j==2)
                   continue f0;
           }
            System.out.println(String.format("i=%s 成功",i));


        }
    }

}
